<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div>
        <input type="text" id="inputD">
    </div>
</body>
<script>
    //防抖,指的是在事件执行n秒之后执行回调,当在n秒内又被触发,则重新计时
    //辅助方式 setTimeout
    let inputD = document.getElementById('inputD');
    //添加事件
    let id = null;
    inputD.addEventListener('keyup', function() {
            //获取输入值    
            let value = inputD.value;
            // mockAjax(value);//没加防抖之前
            clearTimeout(id); //删除计时器,重新计时
            id = setTimeout(() => {
                mockAjax(value)
            }, 500);
        })
        //模拟请求
    function mockAjax(params) {
        console.log('发送请求', params);
    }
</script>

</html>